|
|
|
|
|
|
|
Figure 14.7.
Showing the error message
before exiting the program. |
|
|
|
|
|
|
|
|
Using the Err.Raise Method to Create Your Own Error Conditions |
|
|
|
|
|
|
|
|
Many times it is not practical, or desirable, to display an error message when an error occurs. Other times, you may want to use the error-handling capabilities of Visual Basic 5 to your advantage by creating your own error codes and messages. You can do this using the Raise method of the Err object. Using the Raise method allows you to alert users (or other calling applications) that an error has occurred, but gives both you and the user additional flexibility on how the error is handled. |
|
|
|
|
|
|
|
|
The Raise method takes up to five parameters: |
|
|
|
|
|
|
|
|
·ErrorNumber: This is a unique number identifying the error that just occurred. |
|
|
|
|
|
|
|
|
·ErrorSource: This is the code module that generated the error. |
|
|
|
|
|
|
|
|
·ErrorDescription: This is the text message associated with the error. |
|
|
|
|
|
|
|
|
·HelpFile: This is the help file that contains support information on the error. |
|
|
|
|
|
|
|
|
·HelpcontextID: This is the ID number of the help topic associated with this error. |
|
|
|
|
|
|
|
|
When you raise your own errors, you are required to report an error number. This number can be any unique value. If you use a number already defined as a Visual Basic error, you automatically get the ErrorDescription and any associated HelpFile and HelpContextID information as well. If you generate your own unique number, you can fill in the other parameters yourself. It is recommended that you use the vbObjectError constant as a base number for your own error codes. This guarantees that your error number does not conflict with any Visual Basic errors. |
|
|
|
|
|
|
|
|
Here is a typical call to use the Err.Raise method: |
|
|
|
|
|
|
|
|
'
LocalErr:
' trouble with file stuff! |
|
|
|
|
|